const {remote} = require('electron')
const {ipcRenderer} = require('electron')

let id = document.body.getAttribute("id")

;(function () {
    axios.get("http://localhost:9000/askDonate" + "?" + "id=" + id)
        .then(function (response) {
            document.getElementById("donate_yet").innerHTML = response.data.sum;
            calculateBar(response.data.sum)
        });
    askData()
})();

let username = remote.getGlobal("userinfo").user

document.getElementById("donate").addEventListener('click', () => {
    let check = document.getElementById("checkName").checked
    let money = document.getElementById("donate_money").value
    let addr = createHash(32)
    if (check == true) {
        uploadData("匿名", money, addr)
    } else {
        uploadData(username, money, addr)
    }
    ipcRenderer.send('history', id)
})

function uploadData(username, money, addr) {
    axios.post("http://localhost:9000/addInfo", {
        id: id,
        username: username,
        money: money,
        addr: addr
    }).then(function (response) {
        if (response.status == 200) {
            alert("捐款成功！感谢您的支持！")
            update(username, money, addr)
            //ipcRenderer.send('history', id)
        }
    }).catch(function (error) {
        if (error.response.status == 403 || error.response.status == 503) {
            alert("出错了，请稍后再试！")
        }
    })
}

function askData() {
    axios.get("http://localhost:9000/askInfo" + "?" + "id=" + id)
        .then(function (response) {
            if (response.status == 200) {
                let re = response.data
                SyncInfo(re)
            }
        })
        .catch(function (error) {
            console.log(error)
        })
}

function update(username, money, addr) {
    money = parseInt(money, 10)
    let li_new = document.createElement("li")
    li_new.setAttribute("class", "list-group-item")
    li_new.innerHTML = addr
    document.getElementById("flow").appendChild(li_new)
    let user_new = document.createElement("li")
    user_new.setAttribute("class", "list-group-item")
    let img_new = document.createElement("img")
    img_new.src = "src/open-iconic-master/svg/person.svg"
    user_new.appendChild(img_new)
    let name_new = document.createElement("span")
    name_new.innerHTML = username
    user_new.appendChild(name_new)
    let span_new = document.createElement("span")
    span_new.setAttribute("class", "m")
    span_new.innerHTML = "￥" + money
    user_new.appendChild(span_new)
    document.getElementById("work_people").appendChild(user_new)
    let org = document.getElementById("donate_yet").innerHTML
    let sum = parseInt(org, 10)
    document.getElementById("donate_yet").innerHTML = sum + money
    calculateBar(sum + money)
    let num = document.getElementById("donate_num").innerHTML
    let people = parseInt(num, 10)
    document.getElementById("donate_num").innerHTML = people + 1
}

function calculateBar(money) {
    let goal = document.getElementById("donate_goal").innerHTML
    goal = parseInt(goal, 10)
    let pcent = Math.ceil(money * 100 / goal)
    document.getElementById("bar").style = "width: " + pcent + "%"
    document.getElementById("bar_process").innerHTML = pcent + "% 完成"
}

function createHash (hashLength) {
    if (!hashLength || typeof(Number(hashLength)) != 'number') {return;}
    var ar = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    var hs = [];
    var hl = Number(hashLength);
    var al = ar.length;
    for (var i = 0; i < hl; i ++) {
        hs.push(ar[Math.floor(Math.random() * al)]);
    }

    return hs.join('');
}

function SyncInfo(res) {
    for (let i = 0; i < res.length; i++) {
        let sin = res[i]
        syncInfo(sin.Username, sin.Money, sin.Addr)
    }
}

function syncInfo(username, money, addr) {
    let li_new = document.createElement("li")
    li_new.setAttribute("class", "list-group-item")
    li_new.innerHTML = addr
    document.getElementById("flow").appendChild(li_new)
    let user_new = document.createElement("li")
    user_new.setAttribute("class", "list-group-item")
    let img_new = document.createElement("img")
    img_new.src = "src/open-iconic-master/svg/person.svg"
    user_new.appendChild(img_new)
    let name_new = document.createElement("span")
    name_new.innerHTML = username
    user_new.appendChild(name_new)
    let span_new = document.createElement("span")
    span_new.setAttribute("class", "m")
    span_new.innerHTML = "￥" + money
    user_new.appendChild(span_new)
    document.getElementById("work_people").appendChild(user_new)
}
